package leetcode.code0303;

public class NumArray {

	int[] nums;
	int len;
	long[] preSums;

	public NumArray(int[] nums) {
		this.nums = nums;
		this.len = nums.length;
		this.preSums = new long[len + 1];
		this.preSum();
	}

	public int sumRange(int left, int right) {
		return (int) this.preSum(left, right);
	}

	public long[] preSum() {
		preSums[0] = nums[0];
		for (int i = 1; i < len; i++) {
			preSums[i] = preSums[i - 1] + nums[i];
		}
		return preSums;
	}

	public long preSum(int x, int y) {
		return x == 0 ? preSums[y] : preSums[y] - preSums[x - 1];
	}
}
